package com.jike.jkgame;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.TextView;

import com.jike.jkgame.data.PlayerCompareData;
import com.jike.jkgame.data.PlayerDetail;
import com.jike.jkgame.data.SimpleGeneral;

public class PlayerCompareDialog extends DialogFragment implements
		OnClickListener {

	private PlayerCompareData mData;

	public static PlayerCompareDialog newInstance(PlayerCompareData data) {
		PlayerCompareDialog dialog = new PlayerCompareDialog(data);

		return dialog;
	}

	public PlayerCompareDialog(PlayerCompareData data) {
		mData = data;
	}

	private void bindView(LayoutInflater inflater, View view,
			PlayerDetail player) {
		TextView tv = (TextView) view.findViewById(R.id.player_info);
		tv.setText(player.getFormatedString());

		HorizontalScrollView container = (HorizontalScrollView) view
				.findViewById(R.id.generals);

		for (SimpleGeneral g : player.team) {
			View v = inflater.inflate(R.layout.compare_general_item_layout,
					container);
			tv = (TextView) v.findViewById(R.id.tv_name);
			tv.setText(String.valueOf(g.gid));
			ImageView iv = (ImageView) v.findViewById(R.id.iv_head);

			tv = (TextView) v.findViewById(R.id.tv_info);
			tv.setText(String.format(
					Utilis.getResString(player.attack ? R.string.general_attack_format
							: R.string.general_defense_format), g.value));
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.player_compare_dialog_layout,
				container);

		View playerView = v.findViewById(R.id.players_attack);
		bindView(inflater, playerView, mData.attack);
		
		playerView = v.findViewById(R.id.players_defense);
		bindView(inflater, playerView, mData.defense);
		
		v.findViewById(R.id.btn_close).setOnClickListener(this);
		v.findViewById(R.id.btn_ok).setOnClickListener(this);
		v.findViewById(R.id.btn_change).setOnClickListener(this);
		return v;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_close:
		case R.id.btn_ok:
		case R.id.btn_change:
			dismiss();
			break;
		default:
			break;
		}

	}

}
